
const LCPlugin=require('../libs/LCPlugin');
const axios=require('axios');
const util=require('./../util')
class Plugin extends LCPlugin{
    constructor() {
        super()
        this.keywords = ['#搜索', '#我想看', '#搜', '#看', 'search'];
    }
    message(context,_this) {
        let newMessage=util.keyword(context.content,this.keywords)
        if(newMessage.hasKeyWord){
            axios.get("https://api.liucl.cn/movieSearch",{
                params: {
                    wd:newMessage.message
                }
            }).then(res=>{
                var message="";
                var urls=[];
                if(res.data.success){
                    res.data.list.forEach(element => {
                        urls.push(element.url)
                    });
                    axios.get("https://liucl.cn/yourls-api.php",{
                        params:{
                            signature:"05e2685fc7",
                            action:"shorUrls",
                            from:"self",
                            urls:urls.join(",")
                        }
                    }).then(res2=>{
                        message="根据您的搜索词\""+newMessage.message+"\",找到"+res.data.list.length+"条相关结果：\n"
                        res2.data.forEach((obj,index)=>{
                            message+=(index+1)+".片名："+res.data.list[index].title+"\n"+"链接: "+obj.short_url+"\n"
                        })
                        message+="免费服务，请勿举报.\n"
                        _this.sendMsg( {
                            data:{
                                ...context,
                                to:context.from||context.fromGroup,
                                from:context.to||null,
                                content:message
                            },
                            type:context.type
                        });
                    })
                }else{
                    message+="未找到任何关于\""+newMessage.message+"\"的内容\n你可访问 https://jx.liucl.cn 尝试搜索。";
                    _this.sendMsg({
                        data:{
                            ...context,
                            to:context.from||context.fromGroup,
                            from:context.to||null,
                            content:message
                        },
                        type:context.type
                    });
                }
            }).catch(err=>{
                var message="网络故障，请稍后重试，或者浏览器访问https://jx.liucl.cn获取影视\n";
                _this.sendMsg({
                    data:{
                        ...context,
                        to:context.from||context.fromGroup,
                        from:context.to||null,
                        content:message
                    },
                    type:context.type
                });
            })
            return true;
        }
        return false;
    }
}
exports.plugin = new Plugin();